<HTML>

<HEAD>
        <META NAME="keywords" CONTENT="electronic commerce, ecommerce, ebusiness, e-business, e-commerce, enterprise software, net economy, Netscape, Sun Microsystems, Sun-Netscape Alliance, iPlanet, internet software">
        <META NAME="description" CONTENT="The Sun-Netscape Alliance, a strategic alliance formed by America Online and Sun Microsystems, Inc., is delivering iPlanet e-commerce software and enterprise solutions that enable companies to compete successfully in the Net Economy">
	<META HTTP-EQUIV="Content-Type" content="text/html; charset=ISO-8859-1">
	<META NAME="TEMPLATEBASE" CONTENT="Authored in FrameMaker. Converted to HTML in WebWorks Publisher 2000. manual wdt 1.5">
	<META NAME="LASTUPDATED" CONTENT="09/28/00 13:18:01">
	<TITLE> Core JavaScript Guide 1.5: 6 Functions</TITLE>
</HEAD>




<BODY TEXT="#000000" LINK="#666699" VLINK="#666699" ALINK="#333366" BGCOLOR="#FFFFFF">

<MAINCONTENT>

<NAVIGATIONCONTENT>
   <TABLE WIDTH="100%" BORDER="0" CELLSPACING="4">
      <TR>
         <TD VALIGN="TOP">
            <b><a href="stmtsov.html">
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#993300">Previous</a></FONT>&nbsp;&nbsp;&nbsp;&nbsp;</b>
<a href="contents.html">
<b><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#993300">Contents</a></FONT></b>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="ix.html"><b><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#993300">Index</a></FONT></b>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="obj.html">
<b>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#993300">
Next
</a>
</FONT>
</b>&nbsp;&nbsp;&nbsp;&nbsp;
         </TD>
     </TR>

      <TR>
         <TD VALIGN="TOP" ALIGN="RIGHT" BGCOLOR="#CCCCCC">
            <FONT FACE="Arial, Helvetica, sans-serif" COLOR="#000000" SIZE="2"><B>
             Core JavaScript Guide 1.5</B></FONT>
         </TD>
      </TR>
      <TR>
   </TABLE>
   <BR>
</NAVIGATIONCONTENT>

<BOOKCONTENT>
   <blockquote>
<BR>
<BR>

      <A NAME="1008300"> </A><A NAME=""> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="4" COLOR="#666666"><B>
<zchapnumber>Chapter 6 </zchapnumber>
&nbsp;
</B>
</FONT>


<A NAME="1008302"> </A>
<A NAME="Functions"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="4" COLOR="#000000">
<B>
<zchaptitle>Functions</zchaptitle>
</B>
</FONT>
<BR>
<BR>
<BR>

<A NAME="1008304"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>Functions are one of the fundamental building blocks in JavaScript. A function is a JavaScript procedure&#151;a set of statements that performs a specific task. To use a function, you must first define it; then your script can call it.</zparagraph>
</FONT>
<P>

<A NAME="1009575"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>This chapter contains the following sections:</zparagraph>
</FONT>
<P>
<ul>
<A NAME="1009576"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1><A HREF="fcns.html#1008305"  >Defining Functions</A></zsmartlist1>
</FONT>
<P><A NAME="1009577"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1><A HREF="fcns.html#1013889"  >Calling Functions</A></zsmartlist1>
</FONT>
<P><A NAME="1009764"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1><A HREF="fcns.html#1012766"  >Using the arguments Array</A></zsmartlist1>
</FONT>
<P><A NAME="1009765"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1><A HREF="fcns.html#1008357"  >Predefined Functions</A></zsmartlist1>
</FONT>
<P></ul>

<BR>
<BR>

<A NAME="1008305"> </A>
<A NAME="Defining Functions"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="4">
<zhead1>Defining Functions</zhead1>
<HR SIZE="2" NOSHADE>
</FONT>
<BR>


<A NAME="1009697"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>A function definition consists of the <FONT FACE="courier, couriernew, monospaced size">function</FONT> keyword, followed by</zparagraph>
</FONT>
<P>
<ul>
<A NAME="1008308"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1>The name of the function.</zsmartlist1>
</FONT>
<P><A NAME="1008309"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1>A list of arguments to the function, enclosed in parentheses and separated by commas.</zsmartlist1>
</FONT>
<P><A NAME="1008310"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1>The JavaScript statements that define the function, enclosed in curly braces, { }. The statements in a function can include calls to other functions defined in the current application.</zsmartlist1>
</FONT>
<P></ul>

<A NAME="1008313"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>For example, the following code defines a simple function named <FONT FACE="courier, couriernew, monospaced size">square</FONT>:</zparagraph>
</FONT>
<P>
<A NAME="1009645"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>function square(number) {<br>
&nbsp;&nbsp;&nbsp;return number * number;<br>
}
</zcodeline>
</FONT>
<P>
<A NAME="1009646"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The function <FONT FACE="courier, couriernew, monospaced size">square</FONT> takes one argument, called <FONT FACE="courier, couriernew, monospaced size">number</FONT>. The function consists of one statement that indicates to return the argument of the function multiplied by itself. The <FONT FACE="courier, couriernew, monospaced size">return</FONT> statement specifies the value returned by the function.</zparagraph>
</FONT>
<P>
<A NAME="1009624"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline><FONT FACE="courier, couriernew, monospaced size">return number * number
</FONT></zcodeline>
</FONT>
<P>
<A NAME="1012732"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>All parameters are passed to functions <I>by value</I>; the value is passed to the function, but if the function changes the value of the parameter, this change is not reflected globally or in the calling function. However, if you pass an object as a parameter to a function and the function changes the object's properties, that change is visible outside the function, as shown in the following example:</zparagraph>
</FONT>
<P>
<A NAME="1012733"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>function myFunc(theObject) {<br>
&nbsp;&nbsp;&nbsp;theObject.make="Toyota"<br>
}<br>
<br>
mycar = {make:"Honda", model:"Accord", year:1998};<br>
x=mycar.make; &nbsp;&nbsp;&nbsp;&nbsp;// returns Honda<br>
myFunc(mycar); &nbsp;&nbsp;&nbsp;// pass object mycar to the function<br>
y=mycar.make; &nbsp;&nbsp;&nbsp;&nbsp;// returns Toyota (prop was changed by the function)
</zcodeline>
</FONT>
<P>
<A NAME="1008317"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>A function can be defined based on a condition. For example, given the following function definition:</zparagraph>
</FONT>
<P>
<A NAME="1013418"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>if (num == 0)<br>
{<br>
&nbsp;&nbsp;&nbsp;function myFunc(theObject) {<br>
&nbsp;&nbsp;&nbsp;theObject.make="Toyota"<br>
&nbsp;&nbsp;&nbsp;}<br>
}
</zcodeline>
</FONT>
<P>
<A NAME="1013498"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>the <FONT FACE="courier, couriernew, monospaced size">myFunc</FONT> function is only defined if the variable <FONT FACE="courier, couriernew, monospaced size">num</FONT> equals 0. If <FONT FACE="courier, couriernew, monospaced size">num</FONT> does not equal 0, the function is not defined, and any attempt to execute it will fail.</zparagraph>
</FONT>
<P>

<A NAME="1013781"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>In addition to defining functions as described here, you can also define <FONT FACE="courier, couriernew, monospaced size">Function</FONT> objects, as described in <A HREF="obj.html#1008590"  >"Function Object" on page&nbsp;106</A>.</zparagraph>
</FONT>
<P>

<A NAME="1013775"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>A <I>method</I> is a function associated with an object. You'll learn more about objects and methods in <A HREF="obj.html#1008302"  >Chapter 7, "Working with Objects."</A></zparagraph>
</FONT>
<P>

<A NAME="1013701"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>A function can also be defined inside an expression. This is called a function expression. Typically such a function is anonymous; it does not have to have a name. For example, the function square could have been defined as:</zparagraph>
</FONT>
<P>

<A NAME="1013868"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph><FONT FACE="courier, couriernew, monospaced size">const square = function(number) {return number * number};</FONT></zparagraph>
</FONT>
<P>

<A NAME="1013869"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>This is convenient when passing a function as an argument to another function. The following example shows the map function being defined and then called with an anonymous function as its first parameter:</zparagraph>
</FONT>
<P>
<A NAME="1013886"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>function map(f,a) {<br>
&nbsp;&nbsp;&nbsp;var result=new Array;<br>
&nbsp;&nbsp;&nbsp;for (var i = 0; i != a.length; i++)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result[i] = f(a[i]);<br>
&nbsp;&nbsp;&nbsp;return result;<br>
}
</zcodeline>
</FONT>
<P>
<A NAME="1013902"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The call</zparagraph>
</FONT>
<P>
<A NAME="1013908"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>map(function(x) {return x * x * x}, [0, 1, 2, 5, 10];
</zcodeline>
</FONT>
<P>
<A NAME="1013900"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>returns [0, 1, 8, 125, 1000].</zparagraph>
</FONT>
<P>

<BR>
<BR>

<A NAME="1013889"> </A>
<A NAME="Calling Functions"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="4">
<zhead1>Calling Functions</zhead1>
<HR SIZE="2" NOSHADE>
</FONT>
<BR>


<A NAME="1009651"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>Defining a function does not execute it. Defining the function simply names the function and specifies what to do when the function is called. <I>Calling</I> the function actually performs the specified actions with the indicated parameters. For example, if you define the function <FONT FACE="courier, couriernew, monospaced size">square</FONT>, you could call it as follows.</zparagraph>
</FONT>
<P>
<A NAME="1009664"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>square(5)
</zcodeline>
</FONT>
<P>
<A NAME="1009686"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The preceding statement calls the function with an argument of five. The function executes its statements and returns the value twenty-five.</zparagraph>
</FONT>
<P>

<A NAME="1008325"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The arguments of a function are not limited to strings and numbers. You can pass whole objects to a function, too. The <FONT FACE="courier, couriernew, monospaced size">show_props</FONT> function (defined in <A HREF="obj.html#1008312"  >"Objects and Properties" on page&nbsp;91</A>) is an example of a function that takes an object as an argument.</zparagraph>
</FONT>
<P>

<A NAME="1008330"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>A function can even be recursive, that is, it can call itself. For example, here is a function that computes factorials:</zparagraph>
</FONT>
<P>
<A NAME="1008331"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>function factorial(n) {<br>
&nbsp;&nbsp;&nbsp;if ((n == 0) || (n == 1))<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 1<br>
&nbsp;&nbsp;&nbsp;else {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var result = (n * factorial(n-1) );<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return result<br>
&nbsp;&nbsp;&nbsp;}<br>
}
</zcodeline>
</FONT>
<P>
<A NAME="1012574"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>You could then compute the factorials of one through five as follows:</zparagraph>
</FONT>
<P>
<A NAME="1012645"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>a=factorial(1) // returns 1<br>
b=factorial(2) // returns 2<br>
c=factorial(3) // returns 6<br>
d=factorial(4) // returns 24<br>
e=factorial(5) // returns 120
</zcodeline>
</FONT>
<P>
<BR>
<BR>

<A NAME="1012766"> </A>
<A NAME="Using the arguments Array"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="4">
<zhead1>Using the arguments Array</zhead1>
<HR SIZE="2" NOSHADE>
</FONT>
<BR>


<A NAME="1008338"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The arguments of a function are maintained in an array. Within a function, you can address the arguments passed to it as follows:</zparagraph>
</FONT>
<P>
<A NAME="1008339"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>arguments[<I>i</I>]
</zcodeline>
</FONT>
<P>
<A NAME="1008340"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>where <FONT FACE="courier, couriernew, monospaced size">i</FONT> is the ordinal number of the argument, starting at zero. So, the first argument passed to a function would be <FONT FACE="courier, couriernew, monospaced size">arguments[0]</FONT>. The total number of arguments is indicated by <FONT FACE="courier, couriernew, monospaced size">arguments.length</FONT>.</zparagraph>
</FONT>
<P>

<A NAME="1008343"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>Using the <FONT FACE="courier, couriernew, monospaced size">arguments</FONT> array, you can call a function with more arguments than it is formally declared to accept. This is often useful if you don't know in advance how many arguments will be passed to the function. You can use <FONT FACE="courier, couriernew, monospaced size">arguments.length</FONT> to determine the number of arguments actually passed to the function, and then treat each argument using the <FONT FACE="courier, couriernew, monospaced size">arguments</FONT> array.</zparagraph>
</FONT>
<P>

<A NAME="1009813"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>For example, consider a function that concatenates several strings. The only formal argument for the function is a string that specifies the characters that separate the items to concatenate. The function is defined as follows:</zparagraph>
</FONT>
<P>
<A NAME="1012590"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>function myConcat(separator) {<br>
&nbsp;&nbsp;&nbsp;var result="" // initialize list<br>
&nbsp;&nbsp;&nbsp;// iterate through arguments<br>
&nbsp;&nbsp;&nbsp;for (var i=1; i&lt;arguments.length; i++) {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result += arguments[i] + separator<br>
&nbsp;&nbsp;&nbsp;}<br>
&nbsp;&nbsp;&nbsp;return result<br>
}
</zcodeline>
</FONT>
<P>
<A NAME="1012591"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>You can pass any number of arguments to this function, and it creates a list using each argument as an item in the list.</zparagraph>
</FONT>
<P>
<A NAME="1012592"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>// returns "red, orange, blue, "<br>
myConcat(", ","red","orange","blue")
</zcodeline>
</FONT>
<P><A NAME="1012593"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>// returns "elephant; giraffe; lion; cheetah; "<br>
myConcat("; ","elephant","giraffe","lion", "cheetah")
</zcodeline>
</FONT>
<P><A NAME="1012594"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>// returns "sage. basil. oregano. pepper. parsley. "<br>
myConcat(". ","sage","basil","oregano", "pepper", "parsley")
</zcodeline>
</FONT>
<P>
<A NAME="1009506"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>See the <FONT FACE="courier, couriernew, monospaced size">Function</FONT> object in the <A HREF="http://developer.netscape.com/docs/manuals/js/core/jsref/index.htm"><I>Core JavaScript Reference</I></A> for more information.</zparagraph>
</FONT>
<P>
<P>
<A NAME="1012503"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>
<zheadrunin>JavaScript 1.3 and earlier versions. </zheadrunin>
</B>
</FONT>

<A NAME="1012510"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The <FONT FACE="courier, couriernew, monospaced size">arguments</FONT> array is a property of the <FONT FACE="courier, couriernew, monospaced size">Function</FONT> object and can be preceded by the function name, as follows:</zparagraph>
</FONT>
<P>
<A NAME="1012534"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>functionName.arguments[i]
</zcodeline>
</FONT>
<P>
<BR>
<BR>

<A NAME="1008357"> </A>
<A NAME="Predefined Functions"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="4">
<zhead1>Predefined Functions</zhead1>
<HR SIZE="2" NOSHADE>
</FONT>
<BR>


<A NAME="1009833"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>JavaScript has several top-level predefined functions:</zparagraph>
</FONT>
<P>
<ul>
<A NAME="1009845"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1><FONT FACE="courier, couriernew, monospaced size">eval</FONT></zsmartlist1>
</FONT>
<P><A NAME="1009916"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1><FONT FACE="courier, couriernew, monospaced size">isFinite</FONT></zsmartlist1>
</FONT>
<P><A NAME="1009849"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1><FONT FACE="courier, couriernew, monospaced size">isNaN</FONT></zsmartlist1>
</FONT>
<P><A NAME="1009863"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1><FONT FACE="courier, couriernew, monospaced size">parseInt</FONT> and <FONT FACE="courier, couriernew, monospaced size">parseFloat</FONT></zsmartlist1>
</FONT>
<P><A NAME="1009853"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1><FONT FACE="courier, couriernew, monospaced size">Number</FONT> and <FONT FACE="courier, couriernew, monospaced size">String</FONT></zsmartlist1>
</FONT>
<P><A NAME="1009855"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2">
<B>  <li></B>
<zsmartlist1><FONT FACE="courier, couriernew, monospaced size">encodeURI, decodeURI, encodeURIComponent, and decodeURIComponent </FONT>(all available with Javascript 1.5 and later).</zsmartlist1>
</FONT>
<P></ul>

<A NAME="1009836"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The following sections introduce these functions. See the <A HREF="http://developer.netscape.com/docs/manuals/js/core/jsref/index.htm"><I>Core JavaScript Reference</I></A> for detailed information on all of these functions.</zparagraph>
</FONT>
<P>
<P>
<BR>
<A NAME="1008364"> </A>
<A NAME="eval Function"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="4">
<zhead2>eval Function</zhead2>
</FONT>
<BR>
<BR>



<A NAME="1008365"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The <FONT FACE="courier, couriernew, monospaced size">eval</FONT> function evaluates a string of JavaScript code without reference to a particular object. The syntax of <FONT FACE="courier, couriernew, monospaced size">eval</FONT> is:</zparagraph>
</FONT>
<P>
<A NAME="1008366"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>eval(<I>expr</I>)
</zcodeline>
</FONT>
<P>
<A NAME="1008367"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>where <FONT FACE="courier, couriernew, monospaced size">expr</FONT> is a string to be evaluated.</zparagraph>
</FONT>
<P>

<A NAME="1008368"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>If the string represents an expression, <FONT FACE="courier, couriernew, monospaced size">eval</FONT> evaluates the expression. If the argument represents one or more JavaScript statements, <FONT FACE="courier, couriernew, monospaced size">eval</FONT> performs the statements. Do not call <FONT FACE="courier, couriernew, monospaced size">eval</FONT> to evaluate an arithmetic expression; JavaScript evaluates arithmetic expressions automatically.</zparagraph>
</FONT>
<P>
<P>
<BR>
<A NAME="1009925"> </A>
<A NAME="isFinite Function"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="4">
<zhead2>isFinite Function</zhead2>
</FONT>
<BR>
<BR>



<A NAME="1009926"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The isFinite function evaluates an argument to determine whether it is a finite number. The syntax of <FONT FACE="courier, couriernew, monospaced size">isFinite</FONT> is:</zparagraph>
</FONT>
<P>
<A NAME="1009948"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>isFinite(<I>number</I>)
</zcodeline>
</FONT>
<P>
<A NAME="1009980"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>where <FONT FACE="courier, couriernew, monospaced size">number</FONT> is the number to evaluate.</zparagraph>
</FONT>
<P>

<A NAME="1009996"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>If the argument is <FONT FACE="courier, couriernew, monospaced size">NaN</FONT>, positive infinity or negative infinity, this method returns <FONT FACE="courier, couriernew, monospaced size">false</FONT>, otherwise it returns <FONT FACE="courier, couriernew, monospaced size">true</FONT>.</zparagraph>
</FONT>
<P>

<A NAME="1010011"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The following code checks client input to determine whether it is a finite number.</zparagraph>
</FONT>
<P>
<A NAME="1010012"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>if(isFinite(ClientInput) == true)<br>
{<br>
&nbsp;&nbsp;&nbsp;/* take specific steps */<br>
}
</zcodeline>
</FONT>
<P><P>
<BR>
<A NAME="1008370"> </A>
<A NAME="isNaN Function"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="4">
<zhead2>isNaN Function</zhead2>
</FONT>
<BR>
<BR>



<A NAME="1008371"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The <FONT FACE="courier, couriernew, monospaced size">isNaN</FONT> function evaluates an argument to determine if it is "NaN" (not a number). The syntax of <FONT FACE="courier, couriernew, monospaced size">isNaN</FONT> is:</zparagraph>
</FONT>
<P>
<A NAME="1008372"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>isNaN(<I>testValue</I>)
</zcodeline>
</FONT>
<P>
<A NAME="1008373"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>where <FONT FACE="courier, couriernew, monospaced size">testValue</FONT> is the value you want to evaluate.</zparagraph>
</FONT>
<P>

<A NAME="1008374"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The <FONT FACE="courier, couriernew, monospaced size">parseFloat</FONT> and <FONT FACE="courier, couriernew, monospaced size">parseInt</FONT> functions return "NaN" when they evaluate a value that is not a number. <FONT FACE="courier, couriernew, monospaced size">isNaN</FONT> returns true if passed "NaN," and false otherwise.</zparagraph>
</FONT>
<P>

<A NAME="1008375"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The following code evaluates <FONT FACE="courier, couriernew, monospaced size">floatValue</FONT> to determine if it is a number and then calls a procedure accordingly:</zparagraph>
</FONT>
<P>
<A NAME="1008376"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>floatValue=parseFloat(toFloat)
</zcodeline>
</FONT>
<P><A NAME="1008377"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>if (isNaN(floatValue)) {<br>
&nbsp;&nbsp;&nbsp;notFloat()<br>
} else {<br>
&nbsp;&nbsp;&nbsp;isFloat()<br>
}
</zcodeline>
</FONT>
<P><P>
<BR>
<A NAME="1008379"> </A>
<A NAME="parseInt and parseFloat Functions"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="4">
<zhead2>parseInt and parseFloat Functions</zhead2>
</FONT>
<BR>
<BR>



<A NAME="1008380"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The two "parse" functions, <FONT FACE="courier, couriernew, monospaced size">parseInt</FONT> and <FONT FACE="courier, couriernew, monospaced size">parseFloat</FONT>, return a numeric value when given a string as an argument.</zparagraph>
</FONT>
<P>

<A NAME="1009327"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The syntax of <FONT FACE="courier, couriernew, monospaced size">parseFloat</FONT> is</zparagraph>
</FONT>
<P>
<A NAME="1008387"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>parseFloat(<I>str</I>)
</zcodeline>
</FONT>
<P>
<A NAME="1008388"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>where <FONT FACE="courier, couriernew, monospaced size">parseFloat</FONT> parses its argument, the string <FONT FACE="courier, couriernew, monospaced size">str</FONT>, and attempts to return a floating-point number. If it encounters a character other than a sign (+ or -), a numeral (0-9), a decimal point, or an exponent, then it returns the value up to that point and ignores that character and all succeeding characters. If the first character cannot be converted to a number, it returns "NaN" (not a number).</zparagraph>
</FONT>
<P>

<A NAME="1008389"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The syntax of <FONT FACE="courier, couriernew, monospaced size">parseInt</FONT> is</zparagraph>
</FONT>
<P>
<A NAME="1008390"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>parseInt(str [, radix])
</zcodeline>
</FONT>
<P>
<A NAME="1008391"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph><FONT FACE="courier, couriernew, monospaced size">parseInt</FONT> parses its first argument, the string <FONT FACE="courier, couriernew, monospaced size">str</FONT>, and attempts to return an integer of the specified <FONT FACE="courier, couriernew, monospaced size">radix</FONT> (base), indicated by the second, optional argument, <FONT FACE="courier, couriernew, monospaced size">radix</FONT>. For example, a radix of ten indicates to convert to a decimal number, eight octal, sixteen hexadecimal, and so on. For radixes above ten, the letters of the alphabet indicate numerals greater than nine. For example, for hexadecimal numbers (base 16), A through F are used.</zparagraph>
</FONT>
<P>

<A NAME="1008392"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>If <FONT FACE="courier, couriernew, monospaced size">parseInt</FONT> encounters a character that is not a numeral in the specified radix, it ignores it and all succeeding characters and returns the integer value parsed up to that point. If the first character cannot be converted to a number in the specified radix, it returns "NaN." The <FONT FACE="courier, couriernew, monospaced size">parseInt</FONT> function truncates the string to integer values.</zparagraph>
</FONT>
<P>
<P>
<BR>
<A NAME="1008394"> </A>
<A NAME="Number and String Functions"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="4">
<zhead2>Number and String Functions</zhead2>
</FONT>
<BR>
<BR>



<A NAME="1008395"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The <FONT FACE="courier, couriernew, monospaced size">Number</FONT> and <FONT FACE="courier, couriernew, monospaced size">String</FONT> functions let you convert an object to a number or a string. The syntax of these functions is:</zparagraph>
</FONT>
<P>
<A NAME="1008396"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>Number(<I>objRef</I>)<br>
String(<I>objRef</I>)
</zcodeline>
</FONT>
<P>
<A NAME="1008397"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>where <FONT FACE="courier, couriernew, monospaced size">objRef</FONT> is an object reference.</zparagraph>
</FONT>
<P>

<A NAME="1012662"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The following example converts the <FONT FACE="courier, couriernew, monospaced size">Date</FONT> object to a readable string.</zparagraph>
</FONT>
<P>
<A NAME="1012710"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>D = new Date (430054663215)<br>
// The following returns<br>
// "Thu Aug 18 04:37:43 GMT-0700 (Pacific Daylight Time) 1983"<br>
x = String(D)
</zcodeline>
</FONT>
<P><P>
<BR>
<A NAME="1012712"> </A>
<A NAME="escape and unescape Functions"> </A>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="4">
<zhead2>escape and unescape Functions</zhead2>
</FONT>
<BR>
<BR>



<A NAME="1008402"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The <FONT FACE="courier, couriernew, monospaced size">escape</FONT> and <FONT FACE="courier, couriernew, monospaced size">unescape</FONT> functions let you encode and decode strings. The <FONT FACE="courier, couriernew, monospaced size">escape</FONT> function returns the hexadecimal encoding of an argument in the ISO Latin character set. The <FONT FACE="courier, couriernew, monospaced size">unescape</FONT> function returns the ASCII string for the specified hexadecimal encoding value.</zparagraph>
</FONT>
<P>

<A NAME="1008403"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The syntax of these functions is:</zparagraph>
</FONT>
<P>
<A NAME="1008404"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2">
<zcodeline>escape(string)<br>
unescape(string)
</zcodeline>
</FONT>
<P>
<A NAME="1008405"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>These functions are used primarily with server-side JavaScript to encode and decode name/value pairs in URLs.</zparagraph>
</FONT>
<P>

<A NAME="1013674"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The <FONT FACE="courier, couriernew, monospaced size">escape</FONT> and <FONT FACE="courier, couriernew, monospaced size">unescape </FONT>functions do not work properly for non-ASCII characters and have been deprecated. In JavaScript 1.5 and later, use <FONT FACE="courier, couriernew, monospaced size">encodeURI</FONT>, <FONT FACE="courier, couriernew, monospaced size">decodeURI</FONT>, <FONT FACE="courier, couriernew, monospaced size">encodeURIComponent</FONT>, and <FONT FACE="courier, couriernew, monospaced size">decodeURIComponent</FONT>. </zparagraph>
</FONT>
<P>



   </BLOCKQUOTE>
</BOOKCONTENT>

<FOOTERCONTENT>
   <BLOCKQUOTE>
 <BR>
            <b><a href="stmtsov.html">
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#993300">Previous</a></FONT>&nbsp;&nbsp;&nbsp;&nbsp;</b>
<a href="contents.html">
<b><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#993300">Contents</a></FONT></b>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="ix.html"><b><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#993300">Index</a></FONT></b>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="obj.html">
<b>
<FONT FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#993300">
Next
</a>
</FONT>
</b>&nbsp;&nbsp;&nbsp;&nbsp;
        <HR NOSHADE SIZE="1">


   <font face="Arial, Helvetica, sans-serif" SIZE="1">

Copyright &#169; 2000 <a href="http://www.netscape.com">Netscape Communications Corp.</a> All rights reserved.</FONT>
   </BLOCKQUOTE>
   <FONT FACE="Arial, Helvetica, sans-serif" SIZE="1">
   <P ALIGN="RIGHT">Last Updated <B>September 28, 2000</B>
   </FONT>
   <BR>

</FOOTERCONTENT>

</MAINCONTENT>

</BODY>


</HTML>